package Ttest;

class MyArray<T /*extends Number*/> {
    public Object[] array = new Object[10];
    //public T[] array = new T[10]; 不允许 实例化一个泛型数组
    //public T[] array = (T[])new Object[10];//这样写也不好！！



    public void set(int pos,T val) {
        array[pos] = val;
    }

    public T get(int pos) {
        return (T) array[pos];
    }

    public Object[] getArray() {
        return array;
    }
}

/**
 * 写一个泛型类  实现一个方法 ，这个方法是求指定类型数组的最大值的 ？
 *
 * T extends Comparable<T>  T一定是实现Comparable接口的
 */
class Alg<T extends Comparable<T>> {
    //
    public T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0) {
                max = array[i];
            }
        }
        return max;
    }
}
class A implements Comparable<A>{

    @Override
    public int compareTo(A o) {
        return 0;
    }
}

class Alg2 {
    //泛型方法
    public static <T extends Comparable<T>> T findMax(T[] array) {
        T max = array[0];
        for (int i = 1; i < array.length; i++) {
            if(array[i].compareTo(max) > 0) {
                max = array[i];
            }
        }
        return max;
    }
}

 class Test2 {

   /* public static void main(String[] args) {

        ArrayList<String> arrayList = new ArrayList<>();


        List<String> list = new ArrayList<>();

        List<String> list2 = new Stack<>();
        List<String> list3 = new Vector<>();
        List<String> list4 = new LinkedList<>();

    }*/

    public static void main5(String[] args) {
        //Alg2 alg2 = new Alg2();
        Integer[] array = {1,13,51,71,19};
        Integer ret = Alg2.<Integer>findMax(array);
        System.out.println(ret);
    }

    public static void main4(String[] args) {
        //Alg<String> alg = new Alg<>();

        Alg<Integer> alg2 = new Alg<>();
        Integer[] array = {1,13,51,71,19};

        Integer ret = alg2.findMax(array);
        System.out.println(ret);


        //Alg<A> alg3 = new Alg<>();


    }
/*
    public static void main3(String[] args) {
        //泛型是如何编译的
        MyArray<String> myArray = new MyArray<>();

        myArray.set(0,"hello");

        Object[] ret = myArray.getArray();
        System.out.println(Arrays.toString(ret));
    }

*/



    public static void main1(String[] args) {
        MyArray<String> myArray = new MyArray<>();

        myArray.set(0,"hello");
        //myArray.set(1,90); 这里不能放整型了

        String str = myArray.get(0);
        System.out.println(str);

        MyArray<Integer> myArray2 = new MyArray<>();

        myArray2.set(0,1);
        myArray2.set(1,10);
        //myArray2.set(2,"10");

        Integer a = myArray2.get(0);
        System.out.println(a);
    }
}
